﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.ad
{
    public partial class adclass_edit : Web.UI.ManagePage
    {

        protected string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.adclass().Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("adclass_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                SiteBind(); //绑定站点
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 绑定站点=================================
        private void SiteBind()
        {
            BLL.sites bll = new BLL.sites();
            DataTable dt = bll.GetList(0, "is_lock=0", "sort_id asc,id desc").Tables[0];

            this.ddlSiteId.Items.Clear();
            this.ddlSiteId.Items.Add(new ListItem("请选择站点...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                this.ddlSiteId.Items.Add(new ListItem(dr["title"].ToString(), dr["id"].ToString()));
            }
        }
        #endregion



        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.adclass bll = new BLL.adclass();
            Model.adclass model = bll.GetModel(_id);

            ddlSiteId.SelectedValue = model.site_id.ToString();
            txtTitle.Text = model.title.ToString();
            txtAddTime.Text = model.add_time.ToString();
            
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.adclass model = new Model.adclass();
            BLL.adclass bll = new BLL.adclass();

            model.site_id = int.Parse(ddlSiteId.SelectedValue);
            model.title = txtTitle.Text.ToString();
            model.add_time = Utils.StrToDateTime(txtAddTime.Text.Trim());
 

            if (bll.Add(model) > 0)
            {
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.adclass bll = new BLL.adclass();
            Model.adclass model = bll.GetModel(_id);

            model.site_id = int.Parse(ddlSiteId.SelectedValue);
            model.site_id = int.Parse(ddlSiteId.SelectedValue);
            model.title = txtTitle.Text.ToString();
            model.add_time = Utils.StrToDateTime(txtAddTime.Text.Trim());

 

            if (bll.Update(model))
            {
                result = true;
            }
            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("adclass_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改广告分类成功！", "adclass_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("adclass_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加广告分类成功！", "adclass_list.aspx");
            }
        }

    }
}